import 'package:enjoy_plus_hm/utils/event_bus.dart';
import 'package:enjoy_plus_hm/utils/http.dart';
import 'package:enjoy_plus_hm/utils/toast.dart';
import 'package:flutter/material.dart';

class HouseDetail extends StatefulWidget {
  HouseDetail({super.key, required this.id});

  String id;

  @override
  State<HouseDetail> createState() => _HouseDetailState();
}

class _HouseDetailState extends State<HouseDetail> {
  Map houseInfo = {};

  Widget tagBuilder(int status) {
    List tagList = [
      {},
      {
        "bgColor": const Color.fromARGB(50, 91, 177, 227),
        "textColor": const Color.fromARGB(255, 85, 145, 175),
        "title": "审核中",
      },
      {
        "bgColor": const Color.fromARGB(255, 91, 243, 91),
        "textColor": const Color.fromRGBO(1, 50, 1, 1),
        "title": "审核成功",
      },
      {
        "bgColor": const Color.fromARGB(255, 247, 129, 133),
        "textColor": const Color.fromARGB(255, 84, 1, 1),
        "title": "审核失败",
      },
    ];

    return Container(
      padding: const EdgeInsets.only(left: 5, right: 5, top: 2, bottom: 2),
      decoration: BoxDecoration(
        color: tagList[status]['bgColor'],
        borderRadius: BorderRadius.circular(5),
      ),
      child: Text(
        '${tagList[status]['title']}',
        style: TextStyle(color: tagList[status]['textColor']),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    if (widget.id != '') {
      getHouseDetail(widget.id);
    }
  }

  getHouseDetail(String id) async {
    try {
      var res = await http.get('/room/$id');
      if (res['code'] != 10000) return ToastUtil.showError('获取房源详情失败');
      ToastUtil.showSuccess('获取房源详情成功');
      setState(() {
        houseInfo = res['data'];
      });
    } catch (e) {
      ToastUtil.showError('网络请求出现问题');
    }
  }

  showConfirmDialog(String id) {
    return showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: const Text('温馨提示'),
          content: const Text('确定要删除该房屋吗？'),
          actions: [
            TextButton(
              onPressed: () {
                Navigator.pop(context);
              },
              child: const Text('取消', style: TextStyle(color: Colors.red)),
            ),
            TextButton(
              onPressed: () {
                Navigator.pop(context);
                deleteHouseWithId(id);
              },
              child: const Text('确定'),
            ),
          ],
        );
      },
    );
  }

  deleteHouseWithId(String id) async {
    try {
      final res = await http.delete('/room/$id');
      ToastUtil.showSuccess('删除房源成功');
      //! 刷新房源列表使用了setState，所以需要先返回再触发刷新，否则会报'setState() called after dispose()'
      Navigator.pop(context);
      eventBus.fire(RefreshHouseList());
    } catch (e) {
      ToastUtil.showError('网络请求出现问题 $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('房源详情'), centerTitle: true),
      body:
          houseInfo.isEmpty
              ? const Center(child: CircularProgressIndicator())
              : Stack(
                alignment: Alignment.bottomCenter,
                children: [
                  ListView(
                    padding: const EdgeInsets.only(bottom: 100),
                    children: [
                      // 房屋信息
                      Container(
                        padding: const EdgeInsets.all(10),
                        child: const Text(
                          '房屋信息',
                          style: TextStyle(
                            color: Color.fromARGB(255, 97, 94, 94),
                            fontSize: 16,
                          ),
                        ),
                      ),
                      Container(
                        color: Colors.white,
                        padding: const EdgeInsets.only(
                          left: 10,
                          right: 10,
                          top: 15,
                          bottom: 15,
                        ),
                        child: Row(
                          children: [
                            Expanded(
                              child: Text(
                                '${houseInfo['point']}${houseInfo['building']}',
                              ),
                            ),
                            tagBuilder(houseInfo['status'] as int),
                          ],
                        ),
                      ),
                      // 业主信息
                      Container(
                        padding: const EdgeInsets.all(10),
                        child: const Text(
                          '业主信息',
                          style: TextStyle(
                            color: Color.fromARGB(255, 97, 94, 94),
                            fontSize: 16,
                          ),
                        ),
                      ),
                      Container(
                        color: Colors.white,
                        padding: const EdgeInsets.only(
                          left: 10,
                          right: 10,
                          top: 15,
                          bottom: 15,
                        ),
                        child: Row(
                          children: [
                            const SizedBox(width: 60, child: Text('房间号')),
                            const SizedBox(width: 30),
                            Expanded(child: Text('${houseInfo['room']}室')),
                          ],
                        ),
                      ),
                      Container(
                        color: Colors.white,
                        padding: const EdgeInsets.only(
                          left: 10,
                          right: 10,
                          top: 15,
                          bottom: 15,
                        ),
                        child: Row(
                          children: [
                            const SizedBox(width: 60, child: Text('业主')),
                            const SizedBox(width: 30),
                            Expanded(child: Text('${houseInfo['name']}')),
                          ],
                        ),
                      ),
                      Container(
                        color: Colors.white,
                        padding: const EdgeInsets.only(
                          left: 10,
                          right: 10,
                          top: 15,
                          bottom: 15,
                        ),
                        child: Row(
                          children: [
                            const SizedBox(width: 60, child: Text('手机号')),
                            const SizedBox(width: 30),
                            Expanded(child: Text('${houseInfo['mobile']}')),
                          ],
                        ),
                      ),
                      const SizedBox(height: 30),
                      Container(
                        padding: const EdgeInsets.all(10),
                        color: Colors.white,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            const Text('本人身份证照片'),
                            Image.network(
                              '${houseInfo['idcardFrontUrl']}',
                              height: 200,
                            ),
                            Image.network(
                              '${houseInfo['idcardBackUrl']}',
                              height: 200,
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                  Container(
                    padding: const EdgeInsets.all(10),
                    width: MediaQuery.of(context).size.width,
                    height: 80,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        ElevatedButton(
                          onPressed: () {
                            showConfirmDialog(houseInfo['id']);
                          },
                          child: const Column(
                            children: [
                              SizedBox(height: 8, width: 120),
                              Icon(Icons.delete),
                              Text('删除房屋'),
                              SizedBox(height: 8),
                            ],
                          ),
                        ),
                        ElevatedButton(
                          onPressed: () {
                            Navigator.pushNamed(
                              context,
                              '/house_from',
                              arguments: houseInfo,
                            );
                          },
                          child: const Column(
                            children: [
                              SizedBox(height: 8, width: 120),
                              Icon(Icons.edit),
                              Text('修改房屋'),
                              SizedBox(height: 8),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
    );
  }
}
